<?php
include('admin.php');
set_page_info('id', 'comments');

if (ACTION == 'edit') {
	$id = (int)get_global($_GET, 'id');
	$comment = get_single_item(array(
		'table' => COMMENTS,
		'class' => 'comment',
		'where' => '`comment_id` = "'.$id.'"'
	));
	if (empty($comment)) error('That is not a valid comment.');
	
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		if (empty($c['name'])) $c['name'] = $comment->get_author();
		if (empty($c['email'])) $c['email'] = $comment->get_email();
		if (empty($c['comment'])) $c['comment'] = $comment->get_content(false);
		
		if (!empty($c['status'])) $c['status'] = 1;
		else $c['status'] = 0;
		
		$result = $mysql->update(COMMENTS, array(
			'comment_author' => $c['name'],
			'comment_author_email' => $c['email'],
			'comment_author_site' => $c['website'],
			'comment_content' => $c['comment'],
			'comment_status' => $c['status']
		), '`comment_id` = "'.$comment->get_id().'"');
		
		if ($result === true) header('Location: comments.php#comment-'.$comment->get_id());
		else error('There was an error saving the comment.');
	} else {
		set_page_info('title', 'Editing Comment: #'.$comment->get_id());
		get_header();
?>
		<h2>Editing Comment: #<?php $comment->the_id(); ?></h2>
		<form action="comments.php?action=edit&amp;id=<?php $comment->the_id(); ?>" method="post">
			<p><label for="name">Name</label>
			<input type="text" name="name" id="name" value="<?php $comment->the_author(); ?>" /></p>
			
			<p><label for="email">Email</label>
			<input type="text" name="email" id="email" value="<?php $comment->the_email(); ?>" /></p>
			
			<p><label for="website">Website</label>
			<input type="text" name="website" id="website" value="<?php $comment->the_website(); ?>" /></p>
			
			<p><label for="comment">Comment</label>
			<textarea name="comment" id="comment" rows="6" cols="60"><?php $comment->the_content(false); ?></textarea></p>
			
			<p>
				<label>Status</label>
				<input type="radio" name="status" id="status[0]" class="radio" value="1"<?php if ($comment->get_status()) echo ' checked="checked"'; ?> /> <label for="status[0]" class="radiolabel">Approved</label>
				<input type="radio" name="status" id="status[1]" class="radio" value="0"<?php if (!$comment->get_status()) echo ' checked="checked"'; ?> /> <label for="status[1]" class="radiolabel">Unapproved</label>
			</p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'approve' || ACTION == 'unapprove') {
	$id = (int)get_global($_GET, 'id');
	$result = change_comment_status($id, ACTION);
	
	if ($result === true) header('Location: comments.php#comment-'.$id);
	else error('There was an error changing the status of the comment.');
} elseif (ACTION == 'delete') {
	$id = (int)get_global($_GET, 'id');
	$result = delete_comment($id);
	
	if ($result === true) header('Location: comments.php');
	else error('There was an error deleting the comment.');
} else {
	$approved = get_comments(array(
		'paginationVar' => 'approved'
	), true);
	$unapproved = get_comments(array(
		'where' => '`comment_status` = "0"',
		'paginationVar' => 'unapproved'
	));
	set_page_info('title', 'Manage Comments');
	get_header();
	
	if ($approved->total > 0) {
?>
		<h2>Approved Comments <small>(<?php echo $approved->total; ?>)</small></h2>
		<form action="mass-delete.php?type=comments" method="post">
			<table class="comments" width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Comment</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="3">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $approved->pagination['page']; ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php foreach($approved->items as $comment) { ?>
					<tr<?php $comment->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $comment->the_id(); ?>" /></td>
						<td>
							<p><?php $comment->the_gravatar(true, array('size'=>32, 'class'=>'left marginright')); ?> From <?php $comment->the_author_link(); ?> on <?php $comment->get_post_object()->the_permalink(); ?> | <a href="http://ws.arin.net/whois/?queryinput=<?php $comment->the_ip(); ?>" title="More info..."><?php $comment->the_ip(); ?></a> | <?php $comment->the_date(); ?> <?php $comment->the_permalink(true, array('text'=>'#')); ?></p>
							<?php $comment->the_content(); ?>
						</td>
						<td class="options"><?php $comment->the_options(); ?></td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	}
	
	if ($unapproved->total > 0) {
?>
		<h2>Unapproved Comments <small>(<?php echo $unapproved->total; ?>)</small></h2>
		<form action="mass-delete.php?type=comments" method="post">
			<table class="comments"  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Comment</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="3">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $unapproved->pagination['page']; ?><?php if ($unapproved->needsPagination()) { echo ' &mdash; '; $unapproved->pagination(); } ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php foreach($unapproved->items as $comment) { ?>
					<tr<?php $comment->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $comment->the_id(); ?>" /></td>
						<td>
							<p><?php $comment->the_gravatar(true, array('size'=>32, 'class'=>'left marginright')); ?> From <?php $comment->the_author_link(); ?> on <?php $comment->get_post_object()->the_permalink(); ?> | <?php $comment->the_date(); ?> <?php $comment->the_permalink(true, array('text'=>'#')); ?></p>
							<?php $comment->the_content(); ?>
						</td>
						<td class="options"><?php $comment->the_options(); ?></td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	}
	
	if ($approved->total === 0 && $unapproved->total === 0) {
?>
		<h2>No Comments</h2>
		<form action="mass-delete.php?type=comments" method="post">
			<table  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Comment</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="3">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $approved->pagination['page']; ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
					<tr>
						<td colspan="3">No comments found.</td>
					</tr>
				</tbody>
			</table>
		</form>
<?php
	}
	
	get_footer();
}
?>